<?php
class QAsController extends CController {
	public $defaultAction = 'manage';

	public function filters() {
		return array(
				'accessControl'
		);
	}
	
	
	public function accessRules() {
		return array(
			array(
				'allow',
				'expression' => ForumMembers::isAdmin()
			),
			array(
				'deny',
			),
		);
	}
	
	public function actionManage() {
		$criteria = QAs::model()->topic()->getDbCriteria();
		$with = array('author');
		$count = QAs::model()->with($with)->count($criteria);
		// Pagination
		$pages = new CPagination($count);
		$pages->pageSize = Yii::app()->params['manageListPerPage'];
		$pages->applyLimit($criteria);
		// Get rows
		$qAs = QAs::model()->desc()->with($with)->findAll($criteria);
		$countUnsolved = QAs::model()->topic()->unsolved()->count();
		
		$this->render('qAsManage', array(
				'qAs' => $qAs,
				'count' => $count,
				'countUnsolved' => $countUnsolved,
				'pages' => $pages
			)
		);
	}
	
	public function actionDelete() {
		$qAId = isset($_GET['id']) ? (int)$_GET['id'] : -1;
		
		if($qAId > 0) {
			$qA = QAs::model()->findByPk($qAId);
			if($qA !== null) {
				$criteria = new CDbCriteria;
				$criteria->condition = 'qaParent = '.$qAId;
				$msgs = array();
				if(QAs::model()->deleteAll($criteria) !== false) {
					if($qA->delete()) {
						$msgs = array('save' => '删除成功。');
						Yii::app()->user->setFlash('msgs', $msgs);
						$this->redirect(Yii::app()->request->urlReferrer);
					}
				}
			}
		}
	}
	
	public function actionToggleStatus() {
		$qAId = isset($_GET['id']) ? (int)$_GET['id'] : -1;
		
		if($qAId > 0) {
			$qA = QAs::model()->findByPk($qAId);
			if($qA !== null && $qA->qaParent == QAs::PARENT_TOPIC) {
				if($qA->qaStatus == 1) {
					$qA->qaStatus = 2;
				} else if($qA->qaStatus == 2) {
					$qA->qaStatus = 1;
				}
				if($qA->save()) {
					$message = '咨询状态设置成功。'; 	
					Yii::app()->user->setFlash('message', $message);
					$this->redirect(Yii::app()->request->urlReferrer);
				}
			}
		}
	}

	public function actionEditreplycontent() {
		if(isAjax()) {
			$replyId = isset($_POST['id']) ? $_POST['id'] : null;
			if($replyId !== null) {
				$replyId = (int)substr($replyId, strrpos($replyId, '_')+1);
				$replyContent = isset($_POST['value']) ? addslashes($_POST['value']) : null;
				if($replyContent !== null) {
					$reply = QAs::model()->findByPk($replyId);
					if($replyContent != '') {
						$reply->qaContent = $replyContent;
						if($reply->save()) {
							echo $reply->qaContent;
						}
					} else {
						echo $reply->qaContent;
					}
				}
			}
		}
	}

	public function actionRepliesmanage() {
		$qAId = isset($_GET['id']) ? (int)$_GET['id'] : null;
		Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/assets/jquery.jeditable.mini.js', CClientScript::POS_HEAD);
		if($qAId !== null) {
			$c = new CDbCriteria();
			$c->condition = 'qaParent = '.$qAId;
			$replies = QAs::model()->findAll($c);
			$qa = QAs::model()->find('qaId = ?', array($qAId));

			$this->render('replies-manage', array(
				'replies' => $replies,
				'qa' => $qa
			));
		}
	}
}
